package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.DepartmentService;
import cn.wolfcode.crm.service.EmployeeService;
import cn.wolfcode.crm.service.RoleService;
import cn.wolfcode.crm.utils.JsonResult;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"employeeController:list","员工列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("departments", departments);
        return "/employee/list";
    }

    @RequestMapping("/exportXls")
    @RequiresPermissions(value = {"employeeController:exportXls","员工列表导出"},logical = Logical.OR)
    public void exportXls(HttpServletResponse response) throws Exception {
         //设置响应头
        response.setHeader("content-disposition","attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        // 写回浏览器, 浏览器下载
        workbook.write(response.getOutputStream());
    }

    @RequestMapping("/importXls")
    @RequiresPermissions(value={"employeeController:import", "员工导入"}, logical= Logical.OR)
    public String importXls(MultipartFile file) throws Exception {
        InputStream inputStream = file.getInputStream();
        employeeService.importXls(inputStream);
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"employeeController:delete","员工删除"},logical = Logical.OR)
    public JsonResult delete(Long id) {
      //  int i=1/0;
        try {
            employeeService.deleteByPrimaryKey(id);
        } catch (Exception e) {

            return new JsonResult(false, "删除失败");
        }
        return new JsonResult(true, "删除成功");
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    @RequiresPermissions(value = {"employeeController:batchDelete","员工批量删除"},logical = Logical.OR)
    public JsonResult batchDelete(Long [] ids) {
        try {
            employeeService.batchDelete(ids);
        }catch (Exception e) {

            return new JsonResult(false, "批量删除失败");
        }

        return new JsonResult(true, "批量删除成功");




    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"employeeController:input","员工编辑"},logical = Logical.OR)
    public String input(Model model, Long id) {
        List<Department> departments = departmentService.selectAll();
        List<Role> roles = roleService.selectAll();
        model.addAttribute("departments", departments);
        model.addAttribute("roles", roles);
        if (id != null) {
            Employee employee = employeeService.selectByPrimaryKey(id);
            model.addAttribute("employee", employee);
        }
        return "/employee/input";

    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"employeeController:saveOrUpdate","员工添加或修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Employee employee, Long[] roleIds) {
       try {
           if (employee.getId() != null) {

               employeeService.updateByPrimaryKey(employee, roleIds);
           } else {
               employeeService.insert(employee, roleIds);
           }

       }catch (Exception e){

           return new JsonResult(true, "保存或者新增失败");
       }



        return new JsonResult(true, "保存或者新增成功");

    }


}
